﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class UserAddForm : Form
    {
        private bool addflag = false;

        public UserAddForm()
        {
            InitializeComponent();
        }

        private bool addcheck()
        {
            if (this.textBox1.Text == "")
            {
                MessageBox.Show("请输入用户号码");
                return false;
            }
            if (this.textBox2.Text == "")
            {
                MessageBox.Show("请输入用户姓名");
                return false;
            }
            if (this.comboBox1.SelectedItem == null)
            {
                MessageBox.Show("请选择性别");
                return false;
            }
            if (this.comboBox2.SelectedItem == null)
            {
                MessageBox.Show("请选择用户权限");
                return false;
            }
            return true;
        }

        private bool checklength()
        {
            if (this.textBox1.Text.Length > 50)
            {
                MessageBox.Show("用户号码不能超过50个字符");
                return false;
            }

            if (this.textBox2.Text.Length > 50)
            {
                MessageBox.Show("用户密码不能超过50个字符");
                return false;
            }

            if (this.textBox3.Text.Length > 30)
            {
                MessageBox.Show("身份证号码不能超过30个字符");
                return false;
            }

            if (this.textBox4.Text.Length > 30)
            {
                MessageBox.Show("联系电话不能超过50个字符");
                return false;
            }

            if (this.textBox5.Text.Length > 30)
            {
                MessageBox.Show("电子邮箱不能超过50个字符");
                return false;
            }

            if (this.textBox6.Text.Length > 500)
            {
                MessageBox.Show("工作地址不能超过500个字符");
                return false;
            }

            if (this.textBox7.Text.Length > 500)
            {
                MessageBox.Show("家庭地址不能超过500个字符");
                return false;
            }

            if (this.textBox8.Text.Length > 500)
            {
                MessageBox.Show("备注不能超过1000个字符");
                return false;
            }

            return true;
        }





        private bool userexist()
        {
            string userid = this.textBox1.Text;
            MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();

            var query =  from c in dbcontext.tb_user
                         where c.ID == userid
                         select c.ID;

            if (query.Count() != 0)
            {
                MessageBox.Show("添加的用户号码已存在");
                return false;
            }
            return true;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (addflag == false) 
            {
                return;
            }

            if ((addcheck() == false) || (checklength() == false) || (userexist() == false))
            {
                return;
            }

            string[] insertdata = new string[11];
            insertdata[0] = this.textBox1.Text;
            insertdata[1] = this.textBox2.Text;
            insertdata[2] = this.comboBox1.Items[this.comboBox1.SelectedIndex].ToString();
            insertdata[3] = this.comboBox2.Items[this.comboBox2.SelectedIndex].ToString();
            insertdata[4] = this.textBox3.Text == "" ? "无" : this.textBox3.Text;
            if (this.dateTimePicker1.Checked == false)
            {
                insertdata[5] = "无";
            }
            else
            {
                string s = this.dateTimePicker1.Value.ToString();
                int endindex = s.IndexOf(' ');
                s = s.Substring(0, endindex);
                insertdata[5] = s;
            }
       //     insertdata[5] = this.dateTimePicker1.Checked == false ? "无" : this.dateTimePicker1.Value.ToString();
            insertdata[6] = this.textBox4.Text == "" ? "无" : this.textBox4.Text;
            insertdata[7] = this.textBox5.Text == "" ? "无" : this.textBox5.Text;
            insertdata[8] = this.textBox6.Text == "" ? "无" : this.textBox6.Text;
            insertdata[9] = this.textBox7.Text == "" ? "无" : this.textBox7.Text;
            insertdata[10] = this.textBox8.Text == "" ? "无" : this.textBox8.Text;

            
  
            MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();
            tb_user item = new tb_user();
            item.Password = "888888";
            item.ID = this.textBox1.Text;
            item.Name = this.textBox2.Text;
            item.sex = this.comboBox1.Items[this.comboBox1.SelectedIndex].ToString();
            item.Role = this.comboBox2.Items[this.comboBox2.SelectedIndex].ToString();

            if (this.textBox3.Text != "")
            {
                item.IDcard = this.textBox3.Text;
            }

            if (this.dateTimePicker1.Checked)
            {
                item.Birthday = this.dateTimePicker1.Value;
            }

            if (this.textBox4.Text != "")
            {
                item.Phone = this.textBox4.Text;
            }

            if (this.textBox5.Text != "")
            {
                item.email = this.textBox5.Text;
            }

            if (this.textBox6.Text != "")
            {
                item.Workaddress = this.textBox6.Text;
            }

            if (this.textBox7.Text != "")
            {
                item.Homeaddress = this.textBox7.Text;
            }

            if (this.textBox8.Text != "")
            {
                item.Comment = this.textBox8.Text;
            }

            dbcontext.tb_user.InsertOnSubmit(item);

            string detail = UserLog.builddetailstring(UserLog.usermanagerheader, "添加", insertdata, null);

            try
            {
                dbcontext.SubmitChanges();
                addflag = false;
                UserLog.addusermanagerecord(User.UserID, User.Username, "添加", item.ID, item.Name, "成功", detail);

                var query1 = from c in dbcontext.tb_userauth
                             where c.userID == item.ID 
                             select c;

                if (query1.Count() == 0)
                {
                    tb_userauth item1 = new tb_userauth();
                    item1.userID = item.ID;
                    item1.a = 0;
                    item1.b = 0;
                    item1.c = 0;
                    item1.d = 0;
                    item1.e = 0;

                    dbcontext.tb_userauth.InsertOnSubmit(item1);
                    dbcontext.SubmitChanges();

                }
            }
            
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                UserLog.addusermanagerecord(User.UserID, User.Username, "添加", item.ID, item.Name, "失败", detail);
                return;
            }

            MessageBox.Show("添加用户成功");
            this.Dispose();
        }





        private void UserAddForm_Load(object sender, EventArgs e)
        {
            this.dateTimePicker1.Checked = false;
        }

        private void UserAddForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (addflag == false)
            {
                e.Cancel = false;
                return;
            }
            
            DialogResult r = MessageBox.Show("保存修改？", "关闭", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
            if (r == DialogResult.Yes)
            {
                 button1_Click(sender, e);
                 e.Cancel = true;

                 if (addflag == false)
                 {
                     e.Cancel = false;
                 }    
            }
            else if (r == DialogResult.Cancel)
            {
                e.Cancel = true;

            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            addflag = true;
        }
    }
}
